<?php

/**
 * This is the model class for table "auction".
 *
 * The followings are the available columns in table 'auction':
 * @property integer $idAuction
 * @property integer $idProduct
 * @property string $status
 * @property double $origin_price
 * @property string $Start_time
 * @property double $Auction_price
 * @property integer $Quantity
 * @property integer $idCurrent_user_bid
 * @property integer $idWiner
 * @property string $Remain_time
 *
 * The followings are the available model relations:
 * @property Product $idProduct0
 * @property User $idCurrentUserB
 * @property Bid[] $bs
 */
class Auction extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Auction the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'auction';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idAuction, idProduct, Start_time, Auction_price, Quantity, idCurrent_user_bid, Remain_time', 'required'),
			array('idAuction, idProduct, Quantity, idCurrent_user_bid, idWiner', 'numerical', 'integerOnly'=>true),
			array('origin_price, Auction_price', 'numerical'),
			array('status', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idAuction, idProduct, status, origin_price, Start_time, Auction_price, Quantity, idCurrent_user_bid, idWiner, Remain_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'products' => array(self::BELONGS_TO, 'Product', 'idProduct'),
			'currentUserBid' => array(self::BELONGS_TO, 'User', 'idCurrent_user_bid'),
			'bids' => array(self::HAS_MANY, 'Bid', 'idAuction'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idAuction' => 'Id Auction',
			'idProduct' => 'Id Product',
			'status' => 'Status',
			'origin_price' => 'Origin Price',
			'Start_time' => 'Start Time',
			'Auction_price' => 'Auction Price',
			'Quantity' => 'Quantity',
			'idCurrent_user_bid' => 'Id Current User Bid',
			'idWiner' => 'Id Winer',
			'Remain_time' => 'Remain Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idAuction',$this->idAuction);
		$criteria->compare('idProduct',$this->idProduct);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('origin_price',$this->origin_price);
		$criteria->compare('Start_time',$this->Start_time,true);
		$criteria->compare('Auction_price',$this->Auction_price);
		$criteria->compare('Quantity',$this->Quantity);
		$criteria->compare('idCurrent_user_bid',$this->idCurrent_user_bid);
		$criteria->compare('idWiner',$this->idWiner);
		$criteria->compare('Remain_time',$this->Remain_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getAllAuctionInfo(){
	
		$allAuctions = $this->with('products','currentUserBid')->findAll();
		$arr = array();
		
		foreach( $allAuctions as $auction){
    		// Get information of auction
			$productName = $auction->products->Name;
    		$productImagePath = $auction->products->ImagePath;
    		$origionPrice = $auction->origin_price;
    		$remainTime = $auction->Remain_time;
    		$price = $auction->Auction_price;
    		$currentUserBidName = $auction->currentUserBid->UserName ;
    		
    		$arr = array_merge($arr, 
    		array(array('productName'=> $productName,
    				'productImage'=>$productImagePath,
    				'origionPrice'=>$origionPrice,
    				'remainTime' => $remainTime,
    				'price' => $price,
    				'currentUserBidName' => $currentUserBidName,
    			)));

		}
		return $arr;
		
	}
	
}